﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Board.Filters;
using Board.Models;

namespace Board.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class ManagementController : Controller
    {
        //
        // GET: /Management/
        
        public WebUser currentUser = null;

        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            currentUser = Tools.CurrentUser;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SaveUnit()
        {
            string siteId = Request["siteId"];
            string unitId = Request["unitId"];
            string bedStatus = Request.Params["BedStatus"];
            string bedType = Request.Params["BedType"];
            if (!string.IsNullOrEmpty(bedType))
            {
                bedType = bedType.Replace(",", "-");
            }
            if (!string.IsNullOrEmpty(siteId))
            {
                if ((currentUser.IsAdmin || (currentUser.IsSiteAdmin && currentUser.DefaultSite.SiteId.Equals(siteId)) || (currentUser.IsUnitAdmin && currentUser.DefaultSite.SiteId.Equals(siteId) && currentUser.DefaultUnit.UnitId.Equals(unitId))) )
                {
                    var beds =
                        currentUser.AccessibleBeds.Where(b => b.SiteId.Equals(siteId) && (string.IsNullOrEmpty(unitId) || b.UnitId.Equals(unitId))).ToList();
                    foreach (var bed in beds)
                    {
                        if (!string.IsNullOrEmpty(bedType))
                        {
                            bed.BedType = (bedType).Trim().Trim('-');
                        }else
                        {
                            bed.BedType = bedType;
                        }
                        if (!string.IsNullOrEmpty(bedStatus))
                        {
                            bed.BedStatus = bedStatus;
                        }
                        bed.Save();
                    }
                }else
                {
                    ViewBag.Message = "insufficient privileges";
                }
                if (!string.IsNullOrEmpty(unitId))
                {
                    ViewBag.Unit =
                        currentUser.AccessibleUnits.FirstOrDefault(
                            u => u.SiteId.Equals(siteId) && u.UnitId.Equals(unitId));
                }else
                {
                    ViewBag.Site = currentUser.AccessibleSites.FirstOrDefault(s => s.SiteId.Equals(siteId));
                }
            }
            return View();
        }

        public ActionResult SaveBed()
        {
            string boardId = Request.Params["BoardId"];
            string bedStatus = Request.Params["BedStatus"];
            string bedType = Request.Params["BedType"];
            if (!string.IsNullOrEmpty(bedType))
            {
                bedType = bedType.Replace(",", "-");
            }
            ViewBag.Message = "Information not saved.";
            if (!string.IsNullOrEmpty(boardId))
            {
                try
                {
                    Bed bed = new Bed(long.Parse(boardId));
                    bed.BedStatus = bedStatus;
                    bed.BedType = bedType;
                    bed.Save();
                    Tools.UpdateCurrentUser();
                    return RedirectToAction("Unit",
                                            new
                                                {
                                                    siteId = bed.SiteId,
                                                    unitId = bed.UnitId,
                                                    @bed = bed.BedId,
                                                    message = "Information Saved."
                                                });
                }
                catch
                {
                    ViewBag.Message = "Exception happened.";
                }
            }
            return View();
        }

        public ActionResult Unit()
        {
            string siteId = Request.Params["siteId"];
            string unitId = Request.Params["unitId"];
            string bed = Request.Params["bed"];
            ViewBag.Message = Request.Params["message"];
            //currentUser = Tools.CurrentUser;
            if (currentUser != null)
            {
                if (!string.IsNullOrEmpty(siteId))
                {
                    if (!string.IsNullOrEmpty(unitId))
                    {
                        if (!string.IsNullOrEmpty(bed))
                        {
                            //bed
                            Bed theBed = new Bed(siteId, unitId, bed);
                            if (theBed.Exist && currentUser.HaveAccessTo(theBed))
                            {
                                ViewBag.Bed = theBed;
                            }
                        }else
                        {
                            //unit
                            ViewBag.Unit =
                                currentUser.AccessibleUnits.FirstOrDefault(
                                    u => u.SiteId.Equals(siteId) && u.UnitId.Equals(unitId));
                        }
                    }else
                    {
                        //site
                        ViewBag.Site = currentUser.AccessibleSites.FirstOrDefault(s => s.SiteId.Equals(siteId));
                    }
                }
                ViewBag.CurrentUser = currentUser;
            }
            return View();
        }

    }
}
